import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/login/Login.vue'
import Main from "@/views/main/Main.vue"
import NotFound from '@/views/notFound/NotFound.vue'
import { localCache } from '@/utils/cache'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/:pathMatch(.*)',
      name: 'notFound',
      component: NotFound
    }
  ]
})

// 准备动态加载的路由
export const asyncRoutes = [
  {
    path: '/',
    component: Main,
    name: 'main',
    mata:{
      title: '首页',
      requireAuth: true
    },
    children:[
    ]
  }
]

// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断是否登录
  const token = localCache.getCache('token')
  if (to.path !== '/login' &&  !token) {
    next('/login')
    
  } else {
    next()
  }
})

export default router
